<?php

namespace wxshop;

use app\admin\model\store\Order;
use think\facade\Db;

class ShopOrder extends Common
{
    public static $AccessToken = '';
    public function __construct($appid,$secret){
        if($appid && $secret){
            $this->access_token = $this->getAccessToken($appid, $secret);
        }else{
            return false;
        }
    }

    public function pullOrder($stime,$store_id)
    {
        $order_id = $this->getOrderId($stime);
        if($order_id){
            $url = 'https://api.weixin.qq.com/channels/ec/order/get?access_token='.$this->access_token;
            foreach($order_id as $k=>$v){
                $postdata = ['order_id'=>$v];
                $res = $this->post($url,json_encode($postdata,320));
                $res = json_decode($res,true);
                if($res['errcode'] == 0){
                    $logid = Db::name('store_order_log')->insertGetId(['store_id'=>$store_id,'pl_order_id'=>(int)$v,'create_time'=>time(),'orderdata'=>json_encode($res['order'],320)]);
                    $res = $res['order'];
                    $orderdata['store_id'] = $store_id;
                    $orderdata['order_id'] = $res['order_id'];
                    $orderdata['order_status'] = $res['status'];
                    $orderdata['create_time'] = $res['create_time'];
                    $orderdata['update_time'] = $res['update_time'];
                    $orderdata['product_infos'] = json_encode($res['order_detail']['product_infos'],320);
                    $orderdata['pay_info'] = json_encode($res['order_detail']['pay_info'],320);
                    $orderdata['product_price'] = number_format($res['order_detail']['price_info']['product_price']/100, 2);
                    $orderdata['order_price'] = number_format($res['order_detail']['price_info']['order_price']/100, 2);
                    $orderdata['freight'] = number_format($res['order_detail']['price_info']['freight']/100, 2);
                    $orderdata['discounted_price'] = isset($res['order_detail']['price_info']['discounted_price'])?number_format($res['order_detail']['price_info']['discounted_price']/100, 2):0.00;
                    $orderdata['isdiscounted'] = isset($res['order_detail']['price_info']['is_discounted'])?1:2;
                    $orderdata['user_name'] = $res['order_detail']['delivery_info']['address_info']['user_name'];
                    $orderdata['postal_code'] = $res['order_detail']['delivery_info']['address_info']['postal_code'];
                    $orderdata['province_name'] = $res['order_detail']['delivery_info']['address_info']['province_name'];
                    $orderdata['city_name'] = $res['order_detail']['delivery_info']['address_info']['city_name'];
                    $orderdata['county_name'] = $res['order_detail']['delivery_info']['address_info']['county_name'];
                    $orderdata['detail_info'] = $res['order_detail']['delivery_info']['address_info']['detail_info'];
                    $orderdata['tel_number'] = $res['order_detail']['delivery_info']['address_info']['tel_number'];
                    $orderdata['delivery_product_info'] = json_encode($res['order_detail']['delivery_info']['delivery_product_info'],320);
                    $orderdata['ship_done_time'] = $res['order_detail']['delivery_info']['ship_done_time'];
                    $orderdata['deliver_method'] = $res['order_detail']['delivery_info']['deliver_method'];
                    $orderdata['user_coupon_id'] = isset($res['order_detail']['coupon_info']['user_coupon_id'])?$res['order_detail']['coupon_info']['user_coupon_id']:'';
                    $orderdata['customer_notes'] = $res['order_detail']['ext_info']['customer_notes'];
                    $orderdata['merchant_notes'] = $res['order_detail']['ext_info']['merchant_notes'];
                    $orderdata['finder_id'] = isset($res['order_detail']['ext_info']['finder_id'])?$res['order_detail']['ext_info']['finder_id']:'';
                    $orderdata['live_id'] = isset($res['order_detail']['ext_info']['live_id'])?$res['order_detail']['ext_info']['live_id']:'';
                    $orderdata['order_scene'] = $res['order_detail']['ext_info']['order_scene'];
                    $orderdata['sharer_info'] = json_encode($res['order_detail']['sharer_info'],320);
                    $orderdata['commission_fee'] = isset($res['order_detail']['settle_info']['commission_fee'])?number_format($res['order_detail']['settle_info']['commission_fee']/100, 2):0.00;
                    $orderdata['predict_commission_fee'] = isset($res['order_detail']['settle_info']['predict_commission_fee'])?number_format($res['order_detail']['settle_info']['predict_commission_fee']/100, 2):0.00;
                    $orderdata['sku_sharer_infos'] = json_encode($res['order_detail']['sku_sharer_infos'],320);
                    $orderdata['aftersale_detail'] = json_encode($res['aftersale_detail'],320);
                    $orderdata['openid'] = $res['openid'];
                    $user_id = Db::name('shop_user')->where('openid',$res['openid'])->value('id');
                    $orderdata['user_id'] = $user_id ? $user_id : 0;
                    $info = Db::name('store_order')->where('order_id',$orderdata['order_id'])->find();
                    $true = true;
                    if(isset($info)){
                        if($info['order_status'] == 100){
                            $true = false;
                        }
                        Db::name('store_order')->where('order_id',$orderdata['order_id'])->update($orderdata);
                        Db::name('store_order_log')->where('id',$logid)->update(['order_id'=>$info['order_id']]);
                    }else{
                        $id = Db::name('store_order')->insertGetId($orderdata);
                        Db::name('store_order_log')->where('id',$logid)->update(['order_id'=>$id]);
                    }

                    if($orderdata['order_status'] == 100 && $true){
                        $model =  new Order();
                        $model->endOrder($orderdata['order_id']);
                    }

                }
            }
        }
        return true;
    }

    public function getOrderId($stime,$order_id=[],$next_key = null){
        $url = 'https://api.weixin.qq.com/channels/ec/order/list/get?access_token='.$this->access_token;
        $postdata = ['create_time_range'=>['start_time'=>$stime,'end_time'=>time()],'page_size'=>20];
        if($next_key){
            $postdata['next_key'] = $next_key;
        }
        $res = $this->post($url,json_encode($postdata,320));
        $res = json_decode($res,true);
        if($res['errcode'] == 0 && $res['has_more']){
            $order_id = $order_id + $res['order_id_list'];
            return $this->getOrderId($stime,$order_id);
        }
        if($res['errcode'] == 0){
            $order_id = $order_id + $res['order_id_list'];
        }
        return $order_id;
    }

}